package com.ktjy.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjy.dto.ActivityDTO;
import com.ktjy.pojo.TbActivity;
import com.ktjy.service.ITbActivityService;
import com.ktjy.utils.AjaxResult;
import com.ktjy.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 活动管理 前端控制器
 * </p>
 *
 * @author jjx
 * @since 2025-08-02
 */
@RestController
@RequestMapping("/api/tb/activity")
public class TbActivityController {
    Logger log= LoggerFactory.getLogger(TbActivityController.class);

    @Autowired
    ITbActivityService activityService;

    @GetMapping("/optionselect/{channel}")
    public AjaxResult optionselect(@PathVariable("channel") String channel) {
        ActivityDTO activityDTO=new ActivityDTO();
        activityDTO.setStatus(Constants.STATE_FOUR+"");
        activityDTO.setChannel(channel);
        List<TbActivity> list = activityService.findList(activityDTO);
        return AjaxResult.success(list);
    }

    @GetMapping("/list")
    public AjaxResult list(ActivityDTO activityDTO,
                           @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        IPage<TbActivity> page = activityService.findPage(activityDTO, pageNo, pageSize);
        return AjaxResult.success(page);
    }

    @PostMapping("/save")
    public AjaxResult save(@RequestBody TbActivity tbActivity){
        try {
            boolean ok=activityService.save(tbActivity);
            if(ok){
                return AjaxResult.success("保存成功");
            }
        } catch (Exception e) {
            log.error("TbActivityController中save方法执行报错的。",e);
        }
        return AjaxResult.error("保存失败");
    }

    @PutMapping("/update")
    public AjaxResult update(@RequestBody TbActivity tbActivity){
        try {
            boolean ok=activityService.updateById(tbActivity);
            if(ok){
                return AjaxResult.success("保存成功");
            }
        } catch (Exception e) {
            log.error("TbActivityController中update方法执行报错的。",e);
        }
        return AjaxResult.error("保存失败");
    }

    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id){
        //查询详情
        try {
            TbActivity tbActivity=activityService.getById(id);
            if(tbActivity!=null){
                return AjaxResult.success(tbActivity);
            }
        } catch (Exception e) {
            log.error("TbActivityController中view方法执行报错的。",e);
        }
        return AjaxResult.error();
    }

    @DeleteMapping("/del/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            boolean ok= activityService.removeById(id);
            if(ok){//删除成功
                return AjaxResult.success("删除成功");
            }
        } catch (Exception e) {
            log.error("TbActivityController中del方法执行报错的。",e);
        }
        return AjaxResult.error("删除失败");
    }
}
